package cn.xxd.common;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.regex.Pattern;

public class UserUtil {

    public static String getCurrentUserId(HttpServletRequest request, String authority) {
        String id = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(authority)) {
                    id = cookie.getValue();
                }
            }
        }
        if (id != null) {
            String[] strings = id.split(":");
            if (strings[1] != null) {
                Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
                if (pattern.matcher(strings[1]).matches()) {
                    return strings[1];
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}
